import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/data_posts_test.dart';

class NewSliverDemo03 extends StatefulWidget {
  const NewSliverDemo03({Key? key}) : super(key: key);

  @override
  _NewSliverDemo03State createState() => _NewSliverDemo03State();
}

class _NewSliverDemo03State extends State<NewSliverDemo03> {
  var list = PostsDatas.TEST_POSTS;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            expandedHeight: 300.0,
            stretch: true,
            onStretchTrigger: () {
              return Future<void>.value();
            },
            flexibleSpace: FlexibleSpaceBar(
              stretchModes: const [
                StretchMode.zoomBackground,
                StretchMode.blurBackground,
                StretchMode.fadeTitle
              ],
              centerTitle: true,
              title: Text("NewSliver"),
              background: Stack(
                fit: StackFit.expand, // 这里很重要
                children: [
                  Image.network(
                    "https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg",
                    fit: BoxFit.cover,
                  ),
                  const DecoratedBox(
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                            begin: Alignment(0.0, 0.5),
                            end: Alignment(0.0, 0.0),
                            colors: [Color(0x60000000), Color(0x00000000)])),
                  )
                ],
              ),
            ),
          ),
          buildSliverList()
        ],
      ),
    );
  }

  SliverList buildSliverList() {
    return SliverList(
        delegate: SliverChildBuilderDelegate((context, index) {
      var inf = list[index];
      return Container(
        margin: EdgeInsets.only(top: 10, left: 16, right: 16),
        child: ListTile(
          tileColor: Colors.pinkAccent.shade100,
          minVerticalPadding: 5,
          leading: Icon(
            Icons.wb_sunny,
            color: Colors.amber,
          ),
          title: Text(inf.title),
          subtitle: Text(inf.author),
          trailing: Icon(Icons.keyboard_arrow_right),
        ),
      );
    }, childCount: list.length));
  }
}
